package com.xuecheng.learning.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.base.utils.StringUtil;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.feignclient.TeachplanServiceClient;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.service.LearningService;
import com.xuecheng.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class LearningServiceImpl implements LearningService {

    @Autowired
    MyCourseTablesService myCourseTablesService;
    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    MediaServiceClient mediaServiceClient;
    @Autowired
    TeachplanServiceClient teachplanServiceClient;

    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
//        判断课程是否存在
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null){
            return RestResponse.validfail("课程不存在");
        }

//        如果课程支持试学,不管用户是是否登录直接播放 todo
        Teachplan teachplanisFree = teachplanServiceClient.getTeachplanisFree(teachplanId);
        if (teachplanisFree == null){
            return RestResponse.validfail("课程计划不存在");
        }
        String isPreview = teachplanisFree.getIsPreview();
        if ("1".equals(isPreview)){//支持试学
//            远程调用媒资获取播放地址
            RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
            return playUrlByMediaId;
        }

//         不是试学的,判断用户是否登录??todo 没登录还能进来??
        if (StringUtil.isEmpty(userId)){//用户没登陆
            String charge = coursepublish.getCharge();
            if ("201000".equals(charge)){//免费的
//            有资格学习,返回视频的播放地址
//            远程调用媒资获取播放地址
                RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                return playUrlByMediaId;
            }
            return RestResponse.validfail("该课程要登陆后购买");//付费的
        }
//        判断学习资格(用户登录)
        XcCourseTablesDto learningStatus = myCourseTablesService.getLearningStatus(userId, courseId);
//学习资格，[{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
        String learnStatus = learningStatus.getLearnStatus();
        if ("702002".equals(learnStatus)){
            return RestResponse.validfail("无法学习,没有选课或选课后没有支付");
        }else if ("702003".equals(learnStatus)){
            return RestResponse.validfail("已过期需要申请续期或重新支付");
        }else {
//          有资格学习,返回播放地址
//          远程调用媒资获取播放地址
            RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
            return playUrlByMediaId;
        }












    }
}
